<?php
namespace Home\Controller\Blog;
use Home\Controller\Layout\LayoutController;
class ArticleController extends LayoutController {
	public function index(){
		$m=M('lunbo');
		$res=$m->where('status=1 and auth=1')->limit(5)->select();
		$h=M('b_article');
		$new=$h->order('c_time desc')->limit(3)->select();
		$this->assign('new',$new);
		$this->assign('res',$res);
		$this->display('/blog/article/index');
	}
	public function all(){
		$m=M('b_article');
		$res=$m->where('status=1')->page(I("get.p"))->order('top desc')->limit(5)->select();
		$h=M('b_article');
		$hot=$h->order('hot desc')->limit(5)->select();
		$p=M('b_pinglun');
		$pinglun=$p->select();
		$ping=array();
		foreach($res as &$v){
			foreach($pinglun as $val){
				if($v['id']==$val['aid']){
					$v['pinglun'][]=$val['id'];
				}
			}	
		}
		$a=M('b_article');
		$aa=$a->where('status=1')->select();
		$pages=count($aa);
		// var_dump($pages);die;
		$Page = new \Think\Page($pages,5);
		$show = $Page->show();
		// var_dump($show);die;
		$g=M('g_goods');
		$goods=$g->order('goods_time desc')->limit(3)->select();
		// var_dump($goods);exit;
		$this->assign('goods',$goods);
		$this->assign('pages',$show);
		// $this->assign('pages',$pages);
		$this->assign('res',$res);
		$this->assign('hot',$hot);
		$this->display('/blog/article/all');
	}
	public function article(){
		// echo 'article'.$_GET['id'];
		$m=M('b_article');
		$res=$m->where('id='.I('get.id'))->find();
		$a=M('b_article');
		$as=$a->where('tid='.$res['tid'])->order('hot desc')->limit(4)->select();
		$u=M('user');
		$username=$u->where('id='.$res['uid'])->find();
		$p=M('b_pinglun');
		$pinglun=$p->alias('p')
		->field('p.*,per.name,per.pic')
		->join('left join think_personal per on per.uid=p.uid')
		->where('p.aid='.I('get.id'))
		->select();
		$r=M('b_reply');
		$reply=$r->alias('r')
		->field('r.*,per.name,per.pic')
		->join('left join think_personal per on per.uid=r.uid')
		->select();
		$h=M('b_huifu');
		$huifu=$h->alias('h')
		->field('h.*,per.name,per.pic')
		->join('left join think_personal per on per.uid=h.uid')
		->select();
		// echo '<pre>';
		// var_dump($huifu);exit;
		if(session('home_id')){
			$pe=M('personal');
			$per=$pe->where('uid='.session('home_id'))->find();
			$this->assign('per',$per);
		// var_dump($per);exit;
		}
		$this->assign('huifu',$huifu);
		$this->assign('reply',$reply);
		$this->assign('pinglun',$pinglun);
		$this->assign('username',$username);
		$this->assign('as',$as);
		$this->assign('res',$res);
		$this->display('/blog/article/detail');
	}

	public static function datas(){
        return self::listss(M('b_type')->select(),'0');
    }
    public static function listss($list,$id){
        $ress = [];
        foreach($list as $k=>$v){
            if($v['pid']==$id){
                $v['sub']=self::listss($list,$v['id']);
                $ress[] = $v;
            }
        }
        return $ress;
    }
    public function type(){
    	$m=M('b_article');
    	$res=$m->where('tid='.I('get.id'))->limit(5)->select();  
    	$ress=$m->where('tid='.I('get.id'))->select(); 
    	$pages=count($ress);
		$Page = new \Think\Page($pages,5);
		$show = $Page->show();
		$h=M('b_article');
		$hot=$h->order('hot desc')->limit(5)->select();
		$this->assign('hot',$hot);
		$this->assign('pages',$show);
    	$this->assign('res',$res);
    	$this->display('/blog/type/type');
    }
    public function pinglun(){
    	// var_dump(I('post.'));
    	$m=M('b_pinglun');
    	$m->aid=I('post.aid');
    	$m->contents=I('post.contents');
    	$m->uid=session('home_id');
    	$m->c_time=date('Y_m_d H:i:s',time());
    	if($m->add()){
    		echo '<script>alert("评论成功");window.location.href="/home/blog/article/article?id='.I('post.aid').'"</script>';
    	}else{
    		echo '<script>alert("评论失败");window.location.href="/home/blog/article/article?id='.I('post.aid').'"</script>';
    	}
    }
    public function reply(){
    	// var_dump(I('post.'));
    	$m=M('b_reply');
    	$m->reid=I('post.reid');
    	$m->contents=I('post.contents');
    	$m->uid=session('home_id');
    	$m->c_time=date('Y_m_d H:i:s',time());
    	if($m->add()){
    		echo 'alert("回复成功");window.location.href="/home/blog/article/article?id='.I('post.aid').'"';
    	}else{
    		echo 'alert("回复失败");window.location.href="/home/blog/article/article?id='.I('post.aid').'"';
    	}
    }
    public function huifu(){
    	$m=M('b_huifu');
    	$m->huifuid=I('post.huifuid');
    	$m->contents=I('post.contents');
    	$m->uid=session('home_id');
    	$m->c_time=date('Y_m_d H:i:s',time());
    	if($m->add()){
    		echo 'alert("回复成功");window.location.href="/home/blog/article/article?id='.I('post.aid').'"';
    	}else{
    		echo 'alert("回复失败");window.location.href="/home/blog/article/article?id='.I('post.aid').'"';
    	}
    }
}
?>